package com.chj.comp.service.demo1.controller;

import com.chj.comp.common.core.util.Result;
import com.chj.comp.common.core.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cuihui
 * @since 2022-02-15
 */
@RestController
@RequestMapping("/permission")
@Api(tags = "权限测试接口")
@Slf4j
public class PermissionTestController {

    /**
     * 客户端权限控制
     */
    @GetMapping("/test1")
    @ApiOperation(value = "客户端scope权限控制")
    @PreAuthorize("#oauth2.hasScope('all')")
    public Result test1() {
        return Result.ok("客户端scope权限控制，该接口拥有表oauth_client_details.scope中配置的all权限");
    }

    /**
     * 用户角色控制
     */
    @GetMapping("/test2")
    @ApiOperation(value = "用户角色控制")
    @PreAuthorize("hasRole('admin')")
    public Result test2() {
        return Result.ok("用户角色控制，该接口拥有admin角色权限");
    }

    /**
     * 用户权限控制
     */
    @GetMapping("/test3")
    @ApiOperation(value = "用户权限控制")
    @PreAuthorize("@pms.hasPermission('add')")
    public Result test3() {
        return Result.ok("用户权限控制，该接口拥有add权限，实现类：com.chj.comp.common.security.component.PermissionService，权限对照表：sys_permission");
    }

    @GetMapping("/test4")
    @ApiOperation(value = "测试")
    public void test4() {
        log.info("getClientId----->" + SecurityUtil.getClientId());
        log.info("getScope----->" + SecurityUtil.getScope().toString());
        log.info("getUserId----->" + SecurityUtil.getUserId() + "");
        log.info("getUserName----->" + SecurityUtil.getUserName());
        log.info("getFullName----->" + SecurityUtil.getFullName());
        log.info("getRoles----->" + SecurityUtil.getRoles().toString());
        log.info("getPermissions----->" + SecurityUtil.getPermissions().toString());
    }
}
